#include "ToolbarFrame.h"
#include "SinaContext.h"
#include <QResizeEvent>
namespace Sina
{
	ToolbarFrame::ToolbarFrame(QWidget* parent):
		QFrame(parent),
		m_loadingMovie(":/Sina/BtnLoadingGIF","GIF")
	{
		setObjectName("sinaToolBarFrame");
		setGeometry(0, 365, 230, 32);

		m_mySelfBtn.setParent(this);
		m_mySelfBtn.setObjectName("mySelfBtn");
		m_mySelfBtn.setGeometry(7, 4, 24, 24);
		m_mySelfBtn.setCursor(Qt::PointingHandCursor);
		m_mySelfBtn.setFocusPolicy(Qt::NoFocus);
		m_mySelfBtn.installEventFilter(this);
		m_mySelfBtn.setScaledContents(true);
		m_mySelfBtn.setPixmap(QPixmap(SinaContext::PluginIcon));
		m_mySelfCountLabel.setParent(this);
		m_mySelfCountLabel.setObjectName("countLabel");
		m_mySelfCountLabel.setGeometry(18 ,4 ,15, 14);
		m_mySelfCountLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
		m_mySelfCountLabel.setAlignment(Qt::AlignCenter);
		m_mySelfCountLabel.hide();

		m_toolButtonGroup.addButton(&m_homeBtn,BtnTimeline);
		m_homeBtn.setParent(this);
		m_homeBtn.setObjectName("timelineBtn");
		m_homeBtn.setGeometry(36, 4, 18, 24);
		m_homeBtn.setCursor(Qt::PointingHandCursor);
		m_homeBtn.setFocusPolicy(Qt::NoFocus);
		m_homeBtn.setCheckable(true);
		m_homeBtn.setChecked(true);
		m_homeLoadingLabel.setParent(this);
		m_homeLoadingLabel.setObjectName("loadingLabel");
		m_homeLoadingLabel.setGeometry(36,21,18,7);
		m_homeLoadingLabel.hide();
		m_homeLoadingLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
		m_homeCountLabel.setParent(this);
		m_homeCountLabel.setObjectName("countLabel");
		m_homeCountLabel.setGeometry(44, 4, 15, 14);
		m_homeCountLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
		m_homeCountLabel.setAlignment(Qt::AlignCenter);
		m_homeCountLabel.hide();

		m_toolButtonGroup.addButton(&m_atmeBtn,BtnMention);
		m_atmeBtn.setParent(this);
		m_atmeBtn.setObjectName("atmeBtn");
		m_atmeBtn.setGeometry(60, 4, 18, 24);
		m_atmeBtn.setCursor(Qt::PointingHandCursor);
		m_atmeBtn.setFocusPolicy(Qt::NoFocus);
		m_atmeBtn.setCheckable(true);
		m_atmeLoadingLabel.setParent(this);
		m_atmeLoadingLabel.setObjectName("loadingLabel");
		m_atmeLoadingLabel.setGeometry(60,21,18,7);
		m_atmeLoadingLabel.hide();
		m_atmeLoadingLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
		m_atmeCountLabel.setParent(this);
		m_atmeCountLabel.setObjectName("countLabel");
		m_atmeCountLabel.setGeometry(68, 4, 15, 14);
		m_atmeCountLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
		m_atmeCountLabel.setAlignment(Qt::AlignCenter);
		m_atmeCountLabel.hide();

		m_toolButtonGroup.addButton(&m_directBtn,BtnDirectMessage);
		m_directBtn.setParent(this);
		m_directBtn.setObjectName("directBtn");
		m_directBtn.setGeometry(84, 4, 18, 24);
		m_directBtn.setCursor(Qt::PointingHandCursor);
		m_directBtn.setFocusPolicy(Qt::NoFocus);
		m_directBtn.setCheckable(true);
		m_directLoadingLabel.setParent(this);
		m_directLoadingLabel.setObjectName("loadingLabel");
		m_directLoadingLabel.setGeometry(84,21,18,7);
		m_directLoadingLabel.hide();
		m_directLoadingLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
		m_directCountLabel.setParent(this);
		m_directCountLabel.setObjectName("countLabel");
		m_directCountLabel.setGeometry(92, 4, 15, 14);
		m_directCountLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
		m_directCountLabel.setAlignment(Qt::AlignCenter);
		m_directCountLabel.hide();

		m_toolButtonGroup.addButton(&m_commentBtn,BtnComments);
		m_commentBtn.setParent(this);
		m_commentBtn.setObjectName("commentBtn");
		m_commentBtn.setGeometry(108, 4, 18, 24);
		m_commentBtn.setCursor(Qt::PointingHandCursor);
		m_commentBtn.setFocusPolicy(Qt::NoFocus);
		m_commentBtn.setCheckable(true);
		m_commentLoadingLabel.setParent(this);
		m_commentLoadingLabel.setObjectName("loadingLabel");
		m_commentLoadingLabel.setGeometry(108,21,18,7);
		m_commentLoadingLabel.hide();
		m_commentLoadingLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
		m_commentCountLabel.setParent(this);
		m_commentCountLabel.setObjectName("countLabel");
		m_commentCountLabel.setGeometry(116, 4, 15, 14);
		m_commentCountLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
		m_commentCountLabel.setAlignment(Qt::AlignCenter);
		m_commentCountLabel.hide();

		m_toolButtonGroup.addButton(&m_favBtn,BtnFavorite);
		m_favBtn.setParent(this);
		m_favBtn.setObjectName("favBtn");
		m_favBtn.setGeometry(132, 4, 18, 24);
		m_favBtn.setCursor(Qt::PointingHandCursor);
		m_favBtn.setFocusPolicy(Qt::NoFocus);
		m_favBtn.setCheckable(true);
		m_favLoadingLabel.setParent(this);
		m_favLoadingLabel.setObjectName("loadingLabel");
		m_favLoadingLabel.setGeometry(132,21,18,7);
		m_favLoadingLabel.setAttribute(Qt::WA_TransparentForMouseEvents);
		m_favLoadingLabel.hide();

		m_tweetBtn.setParent(this);
		m_tweetBtn.setObjectName("tweetBtn");
		m_tweetBtn.setGeometry(186, 4, 18, 24);
		m_tweetBtn.setCursor(Qt::PointingHandCursor);
		m_tweetBtn.setFocusPolicy(Qt::NoFocus);

		connect(&m_toolButtonGroup,SIGNAL(buttonClicked(int)),this,SIGNAL(buttonClicked(int)));
	}

//	void ToolbarFrame::buttonClicked(int id)
//	{
//		switch(id)
//		{
//			case BtnMention:
//				emit buttonClicked(BtnMention);
//				return;
//			case BtnComments:
//				emit buttonClicked(BtnComments);
//				return;
//			case BtnDirectMessage:
//				emit buttonClicked(BtnDirectMessage);
//				return;
//			case BtnFavorite:
//				emit buttonClicked(BtnFavorite);
//				return;
//			case BtnTimeline:
//				emit buttonClicked(BtnTimeline);
//				return;
//			default:
//				return;
//		}
//	}

	void ToolbarFrame::resizeEvent(QResizeEvent* e)
	{
		QFrame::resizeEvent(e);
		m_tweetBtn.move(e->size().width() - 8 - m_tweetBtn.width(), m_tweetBtn.y());
	}

	bool ToolbarFrame::eventFilter(QObject *, QEvent* e)
	{
		if(e->type() == QEvent::MouseButtonRelease)
		{
			emit myselfBtnClicked();
			return true;
		}
		return false;
	}

	void ToolbarFrame::showLoading(ToolbarButton button)
	{
		switch(button)
		{
			case BtnTimeline:
				m_homeLoadingLabel.setMovie(&m_loadingMovie);
				m_homeLoadingLabel.show();
				break;
			case BtnMention:
				m_atmeLoadingLabel.setMovie(&m_loadingMovie);
				m_atmeLoadingLabel.show();
				break;
			case BtnDirectMessage:
				m_directLoadingLabel.setMovie(&m_loadingMovie);
				m_directLoadingLabel.show();
				break;
			case BtnFavorite:
				m_favLoadingLabel.setMovie(&m_loadingMovie);
				m_favLoadingLabel.show();
				break;
			default:
				return;
		}
		if(m_loadingMovie.state() == QMovie::NotRunning)
			m_loadingMovie.start();
	}

	void ToolbarFrame::hideLoading(ToolbarButton button)
	{
		switch(button)
		{
			case BtnTimeline:
				m_homeLoadingLabel.clear();
				m_homeLoadingLabel.hide();
				break;
			case BtnMention:
				m_atmeLoadingLabel.clear();
				m_atmeLoadingLabel.hide();
				break;
			case BtnDirectMessage:
				m_directLoadingLabel.clear();
				m_directLoadingLabel.hide();
				break;
			case BtnFavorite:
				m_favLoadingLabel.clear();
				m_favLoadingLabel.hide();
				break;
			default:
				return;
		}

		bool stopLoading = true;
		if(!m_homeLoadingLabel.isHidden())
			stopLoading = false;
		else if(!m_atmeLoadingLabel.isHidden())
			stopLoading = false;
		else if(!m_directLoadingLabel.isHidden())
			stopLoading = false;
		else if(!m_favLoadingLabel.isHidden())
			stopLoading = false;

		if(stopLoading)
			m_loadingMovie.stop();
	}

	void ToolbarFrame::setWarningCount(ToolbarButton button,int count)
	{
		if(count < 0)
			count = 0;
		QLabel* target;
		QWidget* btn;
		switch(button)
		{
			case BtnMyself:
				target = &m_mySelfCountLabel;
				btn = &m_mySelfBtn;
				break;
			case BtnTimeline:
				target = &m_homeCountLabel;
				btn = &m_homeBtn;
				break;
			case BtnMention:
				target = &m_atmeCountLabel;
				btn = &m_atmeBtn;
				break;
			case BtnDirectMessage:
				target = &m_directCountLabel;
				btn = &m_directBtn;
			default:
				return;
		}

		if(count > 0)
		{
			target->show();
			target->setText(QString::number(count));
		}else
		{
			target->clear();
			target->hide();
		}
	}
} // namespace Sina
